package com.cskaoyan.service;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.util.DateUtil;
import com.cskaoyan.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Ramsey
 * @date 2023年05月28日 22:42
 */
public class AdminBrandServiceimpl implements AdminBrandService {
    @Override
    public CommonData query(Integer page, Integer limit, String sort, String order) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        PageHelper.startPage(page, limit);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.setOrderByClause(sort + " " + order);
        List<MarketBrand> marketBrands = mapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrands);
        sqlSession.close();
        return CommonData.data(marketBrandPageInfo);
    }

    @Override
    public List<MarketBrand> selectByBrandId(Integer brandId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        MarketBrand marketBrand = mapper.selectByPrimaryKey(brandId);
        List<MarketBrand> marketBrands = Arrays.asList(marketBrand);
        for (MarketBrand brand : marketBrands) {
            System.out.println(brand);
        }
        return marketBrands;
    }

    @Override
    public List<MarketBrand> selectByBrandName(String name) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        List<MarketBrand> brandList = mapper.selectByBrandName(name);
        for (MarketBrand marketBrand : brandList) {
            System.out.println(marketBrand);
        }
        return brandList;
    }

    @Override
    public void update(MarketBrand marketBrand) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        marketBrand.setUpdateTime(DateUtil.getDate());
        mapper.updateByPrimaryKeySelective(marketBrand);
        System.out.println("brand更新成功");
    }

    @Override
    public void insert(MarketBrand marketBrand) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        mapper.insertSelective(marketBrand);
        marketBrand.setAddTime(DateUtil.getDate());
        marketBrand.setUpdateTime(DateUtil.getDate());
        sqlSession.commit();
        sqlSession.close();
        System.out.println("brand添加成功！");
    }

    @Override
    public MarketBrand selectOne(MarketBrand marketBrand) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        MarketBrand brand = mapper.selectByPrimaryKey(marketBrand.getId());
        return brand;
    }

    @Override
    public MarketBrand insertAndReturnOne(MarketBrand marketBrand) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        mapper.insertSelective(marketBrand);//先插入
        System.out.println("brand添加成功！");
        MarketBrand marketBrand1;//我插入完马上找回来应该不是不行吧

        return null;
    }

    @Override
    public int insertAndReturnKey(MarketBrand marketBrand) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        int key = mapper.insertAndReturnKey(marketBrand);
        return key;
    }

    @Override
    public int delete(int id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        int i = mapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public List<Map> selectByMap() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        List<Map> maps = mapper.selectByMap();

        return maps;
    }
}
